package com.ajk.shop.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ajk.server.exception.ErrorConstant;
import com.ajk.shop.dao.GoodsMapper;
import com.ajk.shop.exception.ShopServerException;
import com.ajk.shop.model.Category;
import com.ajk.shop.model.CommentLabel;
import com.ajk.shop.model.Goods;
import com.ajk.shop.model.GoodsCategory;
import com.ajk.shop.model.GoodsImage;
import com.ajk.shop.model.GoodsSortConfig;
import com.ajk.shop.model.PostageConfig;
import com.ajk.shop.model.SystemCity;
import com.ajk.shop.model.request.GoodsCommentReq;
import com.ajk.shop.model.request.GoodsReq;
import com.ajk.shop.model.response.CategoryRes;
import com.ajk.shop.model.response.GoodsCommentRes;
import com.ajk.shop.model.response.GoodsRes;
import com.ajk.shop.model.response.MarketingActivityRes;
import com.ajk.shop.service.GoodsService;
import com.ajk.shop.utils.FileStoreUtil;


/** 
 * 
 * @author chenxuan
 *
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService{
	
	private static Logger logger = LoggerFactory.getLogger(GoodsServiceImpl.class);
	
	@Autowired
	private GoodsMapper goodsMapper;
	
	@Override
	public void subtractStock(Integer goodsId, Integer num) {
		GoodsRes gr = goodsMapper.getGoodsDetailById(goodsId);
		if(null == gr){
			throw new ShopServerException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "未找到该商品");
		}
		if(gr.getStockStatus() == 1){
			throw new ShopServerException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "该商品库存已经被锁定");
		}
		//是否需要减库存
		if(gr.getSubtract() == 1){
			if(gr.getAmount() > 0){
				goodsMapper.subtractStock(goodsId, num);
			}else{
				throw new ShopServerException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "该商品已经没有库存");
			}
		}
		
	}

	@Override
	public void increaseStock(Integer goodsId, Integer num) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public GoodsCommentReq addGoodsComment(GoodsCommentReq goodsCommentReq) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CategoryRes> getHomeList(GoodsReq goodsReq) {
		
		List<CategoryRes> list = goodsMapper.homeCategoryList();
		for(CategoryRes cr : list){
			GoodsReq gr = new GoodsReq();
			gr.setStart(0);
			gr.setLimit(6);
			gr.setCategoryId(cr.getId());
			if(null != cr.getImage() && !cr.getImage().trim().isEmpty()){
				cr.setImage(FileStoreUtil.getBaseUrl().concat(cr.getImage()));
			}
			List<GoodsRes> goodsList = goodsMapper.findGoodsByCategoryId(gr);
			for(GoodsRes gres : goodsList){
				if(null != gres.getThumbnailImage() && !gres.getThumbnailImage().trim().isEmpty()){
					gres.setThumbnailImage(FileStoreUtil.getBaseUrl().concat(gres.getThumbnailImage()));
				}
			}
			cr.setGoodsList(goodsList);
		}
		return list;
	}
	
	@Override
	public List<CategoryRes> getBannerList(GoodsReq goodsReq) {
		
		List<CategoryRes> list = goodsMapper.homeBannerList();
		for(CategoryRes cr : list){
			if(null != cr.getImage() && !cr.getImage().trim().isEmpty()){
				cr.setImage(FileStoreUtil.getBaseUrl().concat(cr.getImage()));
			}
		}
		return list;
	}

	@Override
	public GoodsRes goodsDetail(GoodsReq goodReq) {
		if(goodReq.getId() == null || goodReq.getId() <=0){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"商品ID不能为空");
		}
		GoodsRes goodsRes = goodsMapper.getGoodsDetailById(goodReq.getId());
		if(goodsRes.getThumbnailImage() != null && !goodsRes.getThumbnailImage().trim().isEmpty()){
			goodsRes.setThumbnailImage(FileStoreUtil.getBaseURL().concat(goodsRes.getThumbnailImage()));
		}
		List<GoodsImage> goodsImageList = goodsMapper.getGoodsImageList(goodReq.getId());
		for(GoodsImage gi : goodsImageList){
			if(null != gi.getImageLarge() && !gi.getImageLarge().trim().isEmpty()){
				gi.setImageLarge(FileStoreUtil.getBaseURL().concat(gi.getImageLarge()));
			}
			gi.setImageSmall(null);
			if(null != gi.getImageSmall() && !gi.getImageSmall().trim().isEmpty()){
				gi.setImageSmall(FileStoreUtil.getBaseURL().concat(gi.getImageSmall()));
			}
		}
		
		goodsRes.setGoodsImageList(goodsImageList);
		List<Category> categoryList = goodsMapper.getGoodsCategoryByGoodsId(goodReq.getId());
		goodsRes.setCategoryList(categoryList);
		return goodsRes;
	}
	
	@Override
	public GoodsRes goodsDetailById(Integer goodsId) {
		if(goodsId == null || goodsId <=0){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"商品ID不能为空");
		}
		GoodsRes goodsRes = goodsMapper.getGoodsDetailById(goodsId);
		if(goodsRes.getThumbnailImage() != null && !goodsRes.getThumbnailImage().trim().isEmpty()){
			goodsRes.setThumbnailImage(FileStoreUtil.getBaseUrl().concat(goodsRes.getThumbnailImage()));
		}
		List<GoodsImage> goodsImageList = goodsMapper.getGoodsImageList(goodsId);
		for(GoodsImage gi : goodsImageList){
			if(null != gi.getImageLarge() && !gi.getImageLarge().trim().isEmpty()){
				gi.setImageLarge(FileStoreUtil.getBaseUrl().concat(gi.getImageLarge()));
			}
			gi.setImageSmall(null);
			if(null != gi.getImageSmall() && !gi.getImageSmall().trim().isEmpty()){
				gi.setImageSmall(FileStoreUtil.getBaseUrl().concat(gi.getImageSmall()));
			}
		}
		goodsRes.setGoodsImageList(goodsImageList);
		return goodsRes;
	}

	@Override
	public List<SystemCity> getAllRegion() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CategoryRes> categoryList() {
		List<CategoryRes> list = goodsMapper.categoryList();
		for(CategoryRes cr : list){
			if(null != cr){
				List<CategoryRes> subList = goodsMapper.categoryListByParentId(cr.getId());
				cr.setSubCategory(subList);
			}
		}
		return list;
	}

	@Override
	public List<GoodsSortConfig> sortList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<GoodsRes> goodsList(GoodsReq goodsReq) {
		List<GoodsRes> list = goodsMapper.goodsList(goodsReq);
		for(GoodsRes gr : list){
			if(gr.getThumbnailImage() != null && !gr.getThumbnailImage().trim().isEmpty()){
				logger.info("gr.getThumbnailImage() : {}",FileStoreUtil.getBaseUrl());
				gr.setThumbnailImage(FileStoreUtil.getBaseUrl().concat(gr.getThumbnailImage()));
			}
		}
		return list;
	}
	
	@Override
	public Integer goodsListCount(GoodsReq goodsReq) {
		Integer count = goodsMapper.goodsListCount(goodsReq);
		return count;
	}

	@Override
	public MarketingActivityRes marketingActivityGoodsList(GoodsReq goodsReq) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CommentLabel> commentLabelList() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<GoodsCommentRes> goodsCommentList(
			GoodsCommentReq goodsCommentReq) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PostageConfig getPostageConfigByProvinceId(Integer provinceId) {
		
		return goodsMapper.getPostageConfigByProvinceId(provinceId);
	}

	@Override
	public List<GoodsRes> managerList(GoodsReq goodsReq) {
		
		List<GoodsRes> list = goodsMapper.managerList(goodsReq);
		
		return list;
	}

	@Override
	public void categoryAdd(Category category) {
		if(category.getTitle() == null){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"分类名称不能为空");
		}
		if(category.getLevel() == null){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"level 不能为空");
		}
		if(1 == category.getLevel()){
			if(category.getParentId() == null ){
				category.setParentId(0);
			}
		}
		if(category.getLevel() == 2){
			if(category.getParentId() == null || category.getParentId() == 0){
				throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"父类分类ID不能为空");
			}
		}
		CategoryRes cr = goodsMapper.getCategoryByTitle(category.getTitle());
		if(null != cr){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"分类名称为:"+category.getTitle()+"已存在");
		}
		goodsMapper.insertCategory(category);
	}

	@Override
	public void categoryUpdate(Category category) {
		if(category.getId() == null){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"ID不能为空");
		}
		if(category.getTitle() == null){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"分类名称不能为空");
		}
		if(category.getLevel() == null){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"level 不能为空");
		}
		if(category.getLevel() == 1){
			if(category.getParentId() == null && category.getParentId() == 0){
				throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"父类分类ID不能为空");
			}
		}
		CategoryRes cr = goodsMapper.getCategoryByTitleAndId(category.getTitle(),category.getId());
		if(null != cr){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"分类名称为:"+category.getTitle()+"已存在");
		}
		goodsMapper.updateCategory(category);
	}

	@Override
	public void categoryDelete(Category category) {
	
		if(category.getId() == null){
			throw new ShopServerException(ErrorConstant.PARAM_ERROR.getErrorCode(),"分类ID不能为空");
		}
		goodsMapper.deleteCategory(category.getId());
	}

	@Override
	public void categoryHomeView(Category category) {
		goodsMapper.categoryHomeView(category);
	}

	@Override
	public void categoryBannerView(Category category) {
		goodsMapper.categoryBannerView(category);
	}

	@Override
	@Transactional
	public Integer insertGoods(Goods domain) {
		Integer maxOrder = goodsMapper.maxSortOrder();
		maxOrder = maxOrder==null?1:maxOrder+1;
		domain.setSortOrder(maxOrder);
		
		goodsMapper.insertGoods(domain);
		
		return domain.getId();
	}

	@Override
	@Transactional
	public Integer updateGoods(Goods domain) {
		Integer count = goodsMapper.updateGoods(domain);
		return count;
	}

	@Override
	@Transactional
	public Integer insertGoodsImage(GoodsImage domain) {
		goodsMapper.insertGoodsImage(domain);
		return domain.getId();
	}

	@Override
	@Transactional
	public Integer updateGoodsImage(GoodsImage domain) {
		Integer count = goodsMapper.updateGoodsImage(domain);
		return count;
	}

	@Override
	public Integer insertGoodsCategory(GoodsCategory domain) {
		goodsMapper.insertGoodsCategory(domain);
		return domain.getId();
	}

	@Override
	public Integer deleteGoodsCategory(Integer id) {
		Integer count = goodsMapper.deleteGoodsCategoryById(id);
		return count;
	}

	@Override
	public List<GoodsCategory> findGoodsCategoryByGoodsId(Integer goodsId) {
		return goodsMapper.findGoodsCategoryByGoodsId(goodsId);
	}
	
	
	
}